<?php
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
/*常量变量*/
class ConstantController extends AdminbaseController{
	
	protected $constant_model;
	
	function _initialize() {
		parent::_initialize();
		$this->constant_model = D("Common/Constant");
	}
	
	function index(){
		$result=$this->constant_model->order(array("listorder"=>"asc"))->select();
		import("Tree");
		$tree=new \Tree();
		$tree->icon=array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
		$tree->nbsp='&nbsp;&nbsp;&nbsp;';
		
		$newtree=array();
		foreach ($result as $value) {
			$newtree[$value['id']]=$value;
		}
		foreach ($result as $n=> $r) {
        	
        	$result[$n]['level'] = $this->_get_level($r['id'], $newtree);
        	$result[$n]['parentid_node'] = ($r['parentid']) ? ' class="child-of-node-' . $r['parentid'] . '"' : '';
        	
            $result[$n]['str_manage'] = '<a href="' . U("Constant/add", array("parentid" => $r['id'])) . '">'.'新增子项'.'</a> | <a target="_blank" href="' . U("Constant/edit", array("id" => $r['id'],"parentid"=>$r['parentid'])) . '">'.'编辑'.'</a> | <a class="js-ajax-delete" href="' . U("Constant/delete", array("id" => $r['id']) ). '">'.L('DELETE').'</a> ';
        }

        $tree->init($result);
        $str = "<tr id='node-\$id' \$parentid_node>
					<td style='padding-left:20px;'><input name='listorders[\$id]' type='text' size='3' value='\$listorder' class='input input-order'></td>
					<td>\$id</td>
					<td>\$spacer\$pname</td>
				    <td>\$pvalue</td>
					<td>\$str_manage</td>
				</tr>";
        $this->assign("trees", $tree->get_tree(0, $str));
		$this->display();
	}
	
    /**
     *  添加
     */
    public function add() {
    	import("Tree");
    	$tree = new \Tree();
    	$parentid = intval(I("get.parentid"));
    	$result = $this->constant_model->order(array("listorder" => "ASC"))->select();
    	foreach ($result as $r) {
    		$r['selected'] = $r['id'] == $parentid ? 'selected' : '';
    		$array[] = $r;
    	}
    	$str = "<option value='\$id' \$selected>\$spacer \$pname</option>";
    	$tree->init($array);
    	$list = $tree->get_tree(0, $str);
    	$this->assign("list", $list);
    	$this->display();
    }
    
    /**
     *  添加
     */
    public function add_post() {
    	if (IS_POST) {
    		if ($this->constant_model->create()) {
    			if ($this->constant_model->add()!==false) {
    				$this->success("添加成功！", U("Constant/index"));
    			} else {
    				$this->error("添加失败！");
    			}
    		} else {
    			$this->error($this->constant_model->getError());
    		}
    	}
    }
	
	function edit(){
		$id=I("get.id");
		$parentid=I("get.parentid");
		$constant=$this->constant_model->where("id=$id")->find();
		
		import("Tree");
    	$tree = new \Tree();
    	$result = $this->constant_model->order(array("listorder" => "ASC"))->select();
    	foreach ($result as $r) {
    		$r['selected'] = $r['id'] == $parentid ? 'selected' : '';
    		$array[] = $r;
    	}
    	$str = "<option value='\$id' \$selected>\$spacer \$pname</option>";
    	$tree->init($array);
    	$list = $tree->get_tree(0, $str);
    	$this->assign("list", $list);
		
		$this->assign("model",$constant);
		$this->display();
	}
	
	function edit_post(){
		if (IS_POST) {
			if ($this->constant_model->create()) {
				if ($this->constant_model->save()!==false) {
					$this->success("保存成功！");
				} else {
					$this->error("保存失败！");
				}
			} else {
				$this->error($this->constant_model->getError());
			}
		}
	}
	
	//排序
	public function listorders() {
		$status = parent::_listorders($this->constant_model);
		if ($status) {
			$this->success("排序更新成功！");
		} else {
			$this->error("排序更新失败！");
		}
	}
	
	//删除
	function delete(){
		if(isset($_POST['ids'])){
			
		}else{
			$id = intval(I("get.id"));
			if ($this->constant_model->delete($id)!==false) {
				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}
		}
	
	}
	/////////////////////////////////////////////////////////////////////////////////
	  /**
     * 获取菜单深度
     * @param $id
     * @param $array
     * @param $i
     */
    protected function _get_level($id, $array = array(), $i = 0) {
    
    	if ($array[$id]['parentid']==0 || empty($array[$array[$id]['parentid']]) || $array[$id]['parentid']==$id){
    		return  $i;
    	}else{
    		$i++;
    		return $this->_get_level($array[$id]['parentid'],$array,$i);
    	}
    
    }
	
}